import PaginationFormat from '@/class/Pagination/format'
import _ from 'lodash'

export default class PaginationBase extends PaginationFormat {
  constructor(scope, option = {}) {
    super(scope, option)
    if (!scope) {
      return
    }
    this.scope = scope
    this.keys = {
      table: option.tableKey || 'tableData',
      filter: option.filterKey || 'filterOptions',
      filterEd: ''
    }
    this.keys.filterEd = this.keys.filter + 'Ed'

    // 向data里注入数据项
    let vueData = this.scope.$options.data
    if (!vueData) {
      vueData = () => {
        return {}
      }
    } else {
      if (!_.isFunction(vueData)) {
        // eslint-disable-next-line no-console
        console.log('data 必须为函数')
      }
    }
    const data = vueData.bind(this.scope)()
    this.scope.$options.data = () => {
      return Object.assign({}, data, {
        [this.keys.table]: {
          data: [],
          loading: false,
          isEmpty: false, // 返回的数据是否为空数组

          // 目前没用
          searchText: '搜索',
          searchLoading: false,
          searchIco: '',
          searchAbled: false,

          reloadLoad: false,
          reloadIcon: '',
          reloadText: '刷新',
          reloadAbled: false,

          operatorLoad: false,
          selectAllValue: false,
          selectNum: 0
        },
        [this.keys.filter]: {},
        [this.keys.filterEd]: {}
      })
    }
    this.scope.$options.data.bind(this.scope)
  }
}
